<template>
  <d2-container>
    <!-- header 查询条件 -->
    <template slot="header">
      <el-form :inline="true" :model="listQuery" size="mini" style="margin-bottom: -18px;">
        <el-form-item label="查询条件">
          <el-input @keyup.enter.native="handleFilter" style="width: 200px;" placeholder="" v-model="listQuery.id" clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="default" @click="handleFilter" icon="el-icon-search">搜 索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button v-if="product_add" @click="handleCreate" type="primary" icon="el-icon-plus">新 增</el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- table表格 -->
    <el-table
      :key='tableKey'
      :data="list"
      v-loading="listLoading"
      element-loading-text="拼命加载中..."
      highlight-current-row
      stripe
      style="width: 100%">
      <el-table-column align="center" label="id" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="supplierId" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.supplierId}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="productCategoryId" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.productCategoryId}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="name" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="productCode" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.productCode}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="saleType" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.saleType}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="purchasePrice" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.purchasePrice}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="salePrice" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.salePrice}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="bazaarPrice" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.bazaarPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="onlineStatus" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.onlineStatus}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="keyword" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.keyword}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="pictureUrl" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.pictureUrl}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="productExplain" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.productExplain}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="createDate" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.createDate}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="modifyDate" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.modifyDate}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button v-if="product_upd" size="mini" type="primary" @click="handleUpdate(scope.row)" icon="el-icon-edit"></el-button>
          <el-button v-if="productr_del" size="mini" type="danger" @click="deletes(scope.row)" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>

    </el-table>
    <!-- footer 分页条 -->
    <template slot="footer">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total" style="margin: -10px;">
      </el-pagination>
    </template>
    <!-- 新增弹框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="id" prop="id">
              <el-input v-model="form.id" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="supplierId" prop="supplierId">
              <el-input v-model="form.supplierId" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="productCategoryId" prop="productCategoryId">
              <el-input v-model="form.productCategoryId" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="name" prop="name">
              <el-input v-model="form.name" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="productCode" prop="productCode">
              <el-input v-model="form.productCode" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="saleType" prop="saleType">
              <el-input v-model="form.saleType" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="purchasePrice" prop="purchasePrice">
              <el-input v-model="form.purchasePrice" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="salePrice" prop="salePrice">
              <el-input v-model="form.salePrice" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="bazaarPrice" prop="bazaarPrice">
              <el-input v-model="form.bazaarPrice" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="onlineStatus" prop="onlineStatus">
              <el-input v-model="form.onlineStatus" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="keyword" prop="keyword">
              <el-input v-model="form.keyword" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="pictureUrl" prop="pictureUrl">
              <el-input v-model="form.pictureUrl" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="productExplain" prop="productExplain">
              <el-input v-model="form.productExplain" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="createDate" prop="createDate">
              <el-input v-model="form.createDate" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="modifyDate" prop="modifyDate">
              <el-input v-model="form.modifyDate" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')" icon="el-icon-close" size="mini">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')" icon="el-icon-check" size="mini">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')" icon="el-icon-check" size="mini">修 改</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
import { fetchList, getObj, addObj, putObj, delObj } from '@/api/product'
import { mapGetters } from 'vuex'
import ElRadioGroup from 'element-ui/packages/radio/src/radio-group'
import ElOption from 'element-ui/packages/select/src/option'
export default {
  components: {
    ElOption,
    ElRadioGroup
  },
  name: 'table_product',
  data () {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      form: {
        id: undefined,
        supplierId: undefined,
        productCategoryId: undefined,
        name: undefined,
        productCode: undefined,
        saleType: undefined,
        purchasePrice: undefined,
        salePrice: undefined,
        bazaarPrice: undefined,
        onlineStatus: undefined,
        keyword: undefined,
        pictureUrl: undefined,
        productExplain: undefined,
        createDate: undefined,
        modifyDate: undefined
      },
      rules: {
        id: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        supplierId: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        productCategoryId: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        productCode: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        saleType: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        purchasePrice: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        salePrice: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        bazaarPrice: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        onlineStatus: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        keyword: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        pictureUrl: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        productExplain: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        createDate: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        modifyDate: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ]
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      tableKey: 0
    }
  },
  computed: {
  },
  filters: {
  },
  created () {
    this.getList()
    this.product_add = this.hasFunctions(['product_add'])
    this.product_upd = this.hasFunctions(['product_upd'])
    this.product_del = this.hasFunctions(['product_del'])
  },
  methods: {
    getList () {
      this.listLoading = true
      this.listQuery.isAsc = false
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter () {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange (val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate () {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate (row) {
      getObj(row.id).then(response => {
        this.form = response.data
        this.dialogFormVisible = true
        this.dialogStatus = 'update'
      })
    },
    create (formName) {
      const set = this.$refs
      this.form.role = this.role
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    cancel (formName) {
      const set = this.$refs
      this.dialogFormVisible = false
      set[formName].resetFields()
    },
    update (formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          this.form.password = undefined
          putObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    deletes (row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'})
        .then(() => {
          delObj(row.id)
            .then(() => {
              this.getList()
              this.$notify({title: '成功', message: '删除成功', type: 'success', duration: 2000})
            })
            .cache(() => {
              this.$notify({title: '失败', message: '删除失败', type: 'error', duration: 2000})
            })
        })
    },
    resetTemp () {
      this.form = {
        id: undefined,
        supplierId: undefined,
        productCategoryId: undefined,
        name: undefined,
        productCode: undefined,
        saleType: undefined,
        purchasePrice: undefined,
        salePrice: undefined,
        bazaarPrice: undefined,
        onlineStatus: undefined,
        keyword: undefined,
        pictureUrl: undefined,
        productExplain: undefined,
        createDate: undefined,
        modifyDate: undefined
      }
    }
  }
}
</script>
